<template>
	<view>
		<view class="wanl-product wanlAccompany">
			<view class="product_warter" :class="dataStyle"
				v-show="dataStyle == 'col-2-10' || dataStyle == 'col-2-15' || dataStyle == 'col-2-20' || dataStyle == 'col-2-25' || dataStyle == 'col-2-30'">
				<view class="wanl-waterfall">
					<view id="wanl-left-cloumn" class="wanl-cloumn">
						<view class="warter left" v-for="(item, index) in leftList" :key="index"
							@tap="handleGoods(item.id)">
							<view class="img-wrap">
								<image class="image" :src="$wanlshop.oss(item.image)" mode="widthFix"></image>
							</view>
							<view class="content padding-bj">
								<view class="text-cut-2">
									<text>{{ item.title }}</text>
								</view>
								<view class="goods-tag">
									<view style="background-color: #F4F4F4;color: #9B9B9B;" class="cu-tag radius sm margin-bottom-xs">专业服务</view>
									<view class="cu-tag radius sm line-green margin-bottom-xs">{{item.tags}}</view>
									<!-- <view class="cu-tag radius sm line-green margin-bottom-xs">次日可约</view> -->
								</view>
								<view class="flex align-center justify-between">
									<view class="text-red text-bold text-lg">
										<text class="text-price">{{ item.price }}</text>
									</view>
									<view class="text-sm wanl-gray">
										<text style="color: #9B9B9B;">已售 {{$wanlshop.toFormat(item.sale, 'hundred')}} </text>
									</view>
								</view>
							</view>
						</view>
					</view>
					<view id="wanl-right-cloumn" class="wanl-cloumn">
						<view class="warter right" v-for="(item, index) in rightList" :key="index"
							@tap="handleGoods(item.id)">
							<view class="img-wrap">
								<image class="image" :src="$wanlshop.oss(item.image)" mode="widthFix"></image>
							</view>
							<view class="content padding-bj">
								<view class="text-cut-2">
									<text>{{ item.title }}</text>
								</view>
								<view class="goods-tag">
									<view style="background-color: #F4F4F4;color: #9B9B9B;" class="cu-tag radius sm margin-bottom-xs">专业服务</view>
									<view class="cu-tag radius sm line-green margin-bottom-xs">{{item.tags}}</view>
									<!-- <view class="cu-tag radius sm line-green margin-bottom-xs">次日可约</view> -->
								</view>
								<view class="flex align-center justify-between">
									<view class="text-red text-bold text-lg">
										<text class="text-price">{{ item.price }}</text>
									</view>
									<view class="text-sm wanl-gray">
										<text style="color: #9B9B9B;">已售 {{$wanlshop.toFormat(item.sale, 'hundred')}} </text>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</view>
			<!-- 普通布局 -->
			<view class="product_list" :class="dataStyle" v-show="dataStyle == 'col-1-10'">
				<view class="item align-center" v-for="(item, index) in dataList" :key="index" @tap="handleGoods(item.id)">
					<view class="img-wrap" style="width: 130rpx;height: 130rpx;border-radius: 10rpx;">
						<image style="width: 130rpx;height: 130rpx;border-radius: 10rpx;" :src="$wanlshop.oss(item.image)" mode="aspectFill"></image>
					</view>
					<view class="content padding-tb-sm padding-left-sm">
						<view class="">
							<view class="text-cut-1">
								<text class="text-l text-bold">{{ item.title }}</text>
							</view>
							<view class="goods-tag">
								<view style="background-color: #F4F4F4;color: #9B9B9B;" class="cu-tag radius sm">专业服务</view>
								<view class="cu-tag radius sm line-green">{{item.tags}}</view>
								<!-- <view class="cu-tag radius sm line-green">次日可约</view> -->
							</view>
						</view>
						<view class="flex align-center">
							<view class="text-red text-bold text-lg">
								<text class="text-price">{{ item.price }}</text>
							</view>
							<view class="text-xs margin-left-sm">
								<text style="color: #9B9B9B;">已售 {{$wanlshop.toFormat(item.sale, 'hundred')}}</text>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	/**
	 * wanlAccompany 陪护
	 * @property {Array} dataList 列表数据
	 * @property {String} dataStyle 列表样式
	 * @example <wanl-product />
	 */
	export default {
		name: 'wanlAccompany',
		props: {
			dataList: {
				type: Array,
				required: true,
				default: function() {
					return [];
				}
			},
			dataStyle: {
				type: String,
				default: 'col-1-10'
			},
			dataType: {
				type: String,
				default: 'goods'
			}
		},
		data() {
			return {
				addTime: 180,
				leftList: [],
				rightList: [],
				tempList: [],
				children: []
			}
		},
		watch: {
			copyFlowList(nVal, oVal) {
				// 取差值，即这一次数组变化新增的部分
				let startIndex = Array.isArray(oVal) && oVal.length > 0 ? oVal.length : 0;
				if (nVal.slice(startIndex).length == 0) {
					this.leftList = [];
					this.rightList = [];
					this.tempList = this.cloneData(this.copyFlowList);
				}else{
					this.tempList = this.tempList.concat(this.cloneData(nVal.slice(startIndex)));
				}
				this.splitData();
			},
		},
		mounted() {
			this.tempList = this.cloneData(this.copyFlowList);
			this.splitData();
		},
		computed: {
			// 破坏flowList变量的引用，否则watch的结果新旧值是一样的
			copyFlowList() {
				return this.cloneData(this.dataList);
			}
		},
		methods: {
			async splitData() {
				if (!this.tempList.length) return;
				let rightRect = await this.getRect('#wanl-right-cloumn');
				let leftRect = await this.getRect('#wanl-left-cloumn');
				// 如果左边小于或等于右边，就添加到左边，否则添加到右边
				let item = this.tempList[0];
				if(!item) return ;
				item.image = this.$wanlshop.oss(item.image, 172, 0, 1);
				item.comment = this.$wanlshop.toFormat(item.comment, 'hundred');
				item.praise = item.comment > 0 ? parseInt((item.praise / item.comment) * 100) : 0;
				if (leftRect.height < rightRect.height) {
					this.leftList.push(item);
				} else if (leftRect.height > rightRect.height) {
					this.rightList.push(item);
				} else {
					// 这里是为了保证第一和第二张添加时，左右都能有内容
					// 因为添加第一张，实际队列的高度可能还是0，这时需要根据队列元素长度判断下一个该放哪边
					if (this.leftList.length <= this.rightList.length) {
						this.leftList.push(item);
					} else {
						this.rightList.push(item);
					}
				}
				// 移除临时列表的第一项
				this.tempList.splice(0, 1);
				// 如果临时数组还有数据，继续循环
				if (this.tempList.length) {
					setTimeout(() => {
						this.splitData();
					}, this.addTime)
				}
			},
			// 复制而不是引用对象和数组
			cloneData(data) {
				return JSON.parse(JSON.stringify(data));
			},
			getRect(selector, all) {
				return new Promise(resolve => {
					let query = null;
					// 支付宝小程序不能加后面的.in(this)，是它自身的限制
					// #ifndef MP-ALIPAY
					query = uni.createSelectorQuery().in(this)
					// #endif
					// #ifdef MP-ALIPAY
					query = uni.createSelectorQuery()
					// #endif
					query[all ? 'selectAll' : 'select'](selector)
						.boundingClientRect(rect => {
							if (all && Array.isArray(rect) && rect.length) {
								resolve(rect)
							}
							if (!all && rect) {
								resolve(rect)
							}
						})
						.exec()
				})
			},
			handleGoods(id) {
				this.$wanlshop.to(`/pages/medicalcare/accompany/product?id=${id}`)
			}
		}
	};
</script>

<style lang="scss" scoped>
	.wanl-waterfall {
		display: flex;
		flex-direction: row;
		align-items: flex-start;
		.wanl-cloumn {
			display: flex;
			flex: 1;
			flex-direction: column;
			height: auto;
			width: 50%;
			.wanl-image {
				width: 100%;
			}
		}
	}
	.wanlAccompany .product_list.col-1-10 {
		margin: 0;
		padding-top: 20rpx;
	}
	.wanlAccompany .product_list.col-1-10 .item {
		margin-bottom: 20rpx;
		border-radius: 0;
		padding: 0 30rpx;
	}
</style>
